﻿using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

// Token: 0x02000286 RID: 646
public class MaterialCustoms : MonoBehaviour
{
	// Token: 0x060010BE RID: 4286 RVA: 0x0006A5AC File Offset: 0x000687AC
	private void Awake()
	{
		this.Setup();
	}

	// Token: 0x060010BF RID: 4287 RVA: 0x0006A5B4 File Offset: 0x000687B4
	private void Setup()
	{
		Renderer[] componentsInChildren = base.GetComponentsInChildren<Renderer>(true);
		this.datas = new MaterialCustoms.Data_Base[this.parameters.Length];
		for (int i = 0; i < this.parameters.Length; i++)
		{
			MaterialCustoms.Parameter parameter = this.parameters[i];
			if (parameter.type == MaterialCustoms.Parameter.TYPE.FLOAT01)
			{
				this.datas[i] = new MaterialCustoms.Data_Float(parameter, componentsInChildren, 0f, 1f);
			}
			else if (parameter.type == MaterialCustoms.Parameter.TYPE.FLOAT11)
			{
				this.datas[i] = new MaterialCustoms.Data_Float(parameter, componentsInChildren, -1f, 1f);
			}
			else if (parameter.type == MaterialCustoms.Parameter.TYPE.COLOR)
			{
				this.datas[i] = new MaterialCustoms.Data_Color(parameter, componentsInChildren);
			}
			else if (parameter.type == MaterialCustoms.Parameter.TYPE.ALPHA)
			{
				this.datas[i] = new MaterialCustoms.Data_Alpha(parameter, componentsInChildren);
			}
		}
	}

	// Token: 0x060010C0 RID: 4288 RVA: 0x0006A68C File Offset: 0x0006888C
	private void OnValidate()
	{
		if (Application.isPlaying)
		{
		}
	}

	// Token: 0x060010C1 RID: 4289 RVA: 0x0006A698 File Offset: 0x00068898
	private void Update()
	{
	}

	// Token: 0x060010C2 RID: 4290 RVA: 0x0006A69C File Offset: 0x0006889C
	public void SetColor(string name, Color value)
	{
		for (int i = 0; i < this.datas.Length; i++)
		{
			if (this.datas[i].param.type == MaterialCustoms.Parameter.TYPE.COLOR && this.datas[i].param.name == name)
			{
				MaterialCustoms.Data_Color data_Color = this.datas[i] as MaterialCustoms.Data_Color;
				data_Color.SetValue(value);
			}
		}
	}

	// Token: 0x060010C3 RID: 4291 RVA: 0x0006A70C File Offset: 0x0006890C
	public void SetFloat(string name, float value)
	{
		for (int i = 0; i < this.datas.Length; i++)
		{
			if (this.datas[i].param.name == name)
			{
				if (this.datas[i].param.type == MaterialCustoms.Parameter.TYPE.FLOAT01 || this.datas[i].param.type == MaterialCustoms.Parameter.TYPE.FLOAT01)
				{
					MaterialCustoms.Data_Float data_Float = this.datas[i] as MaterialCustoms.Data_Float;
					data_Float.SetValue(value);
				}
				else if (this.datas[i].param.type == MaterialCustoms.Parameter.TYPE.ALPHA)
				{
					MaterialCustoms.Data_Alpha data_Alpha = this.datas[i] as MaterialCustoms.Data_Alpha;
					data_Alpha.SetValue(value);
				}
			}
		}
	}

	// Token: 0x060010C4 RID: 4292 RVA: 0x0006A7C8 File Offset: 0x000689C8
	public void Load(string filePath)
	{
		CustomDataListLoader customDataListLoader = new CustomDataListLoader();
		customDataListLoader.Load(filePath);
		int attributeNo = customDataListLoader.GetAttributeNo("Name");
		int attributeNo2 = customDataListLoader.GetAttributeNo("Type");
		int attributeNo3 = customDataListLoader.GetAttributeNo("MaterialName");
		int attributeNo4 = customDataListLoader.GetAttributeNo("PropertyName");
		this.parameters = new MaterialCustoms.Parameter[customDataListLoader.GetDataNum()];
		for (int i = 0; i < customDataListLoader.GetDataNum(); i++)
		{
			string data = customDataListLoader.GetData(attributeNo, i);
			string data2 = customDataListLoader.GetData(attributeNo2, i);
			string data3 = customDataListLoader.GetData(attributeNo3, i);
			string data4 = customDataListLoader.GetData(attributeNo4, i);
			MaterialCustoms.Parameter.TYPE type = MaterialCustoms.Parameter.TYPE.FLOAT01;
			if (data2.Equals("Alpha", StringComparison.OrdinalIgnoreCase))
			{
				type = MaterialCustoms.Parameter.TYPE.ALPHA;
			}
			else if (data2.Equals("Color", StringComparison.OrdinalIgnoreCase))
			{
				type = MaterialCustoms.Parameter.TYPE.COLOR;
			}
			else if (data2.Equals("Float01", StringComparison.OrdinalIgnoreCase))
			{
				type = MaterialCustoms.Parameter.TYPE.FLOAT01;
			}
			else if (data2.Equals("Float11", StringComparison.OrdinalIgnoreCase))
			{
				type = MaterialCustoms.Parameter.TYPE.FLOAT11;
			}
			string[] materialNames = data3.Split(new char[]
			{
				','
			}, StringSplitOptions.RemoveEmptyEntries);
			this.parameters[i] = new MaterialCustoms.Parameter(data, type, materialNames, data4);
		}
	}

	// Token: 0x060010C5 RID: 4293 RVA: 0x0006A900 File Offset: 0x00068B00
	public void Save(string filePath)
	{
		FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write);
		if (fileStream == null)
		{
			MonoBehaviour.print("读取失败: " + filePath);
			return;
		}
		StreamWriter streamWriter = new StreamWriter(fileStream);
		streamWriter.Write("Name");
		streamWriter.Write("\t");
		streamWriter.Write("Type");
		streamWriter.Write("\t");
		streamWriter.Write("MaterialName");
		streamWriter.Write("\t");
		streamWriter.Write("PropertyName");
		streamWriter.WriteLine();
		if (this.parameters != null)
		{
			for (int i = 0; i < this.parameters.Length; i++)
			{
				MaterialCustoms.Parameter parameter = this.parameters[i];
				string text = string.Empty;
				for (int j = 0; j < parameter.materialNames.Length; j++)
				{
					text += parameter.materialNames[j];
					if (j < parameter.materialNames.Length - 1)
					{
						text += ",";
					}
				}
				streamWriter.Write(parameter.name);
				streamWriter.Write("\t");
				streamWriter.Write(parameter.type);
				streamWriter.Write("\t");
				streamWriter.Write(text);
				streamWriter.Write("\t");
				streamWriter.Write(parameter.propertyName);
				streamWriter.Write("\t");
				streamWriter.WriteLine();
			}
		}
		streamWriter.Close();
		fileStream.Close();
	}

	// Token: 0x060010C6 RID: 4294 RVA: 0x0006AA78 File Offset: 0x00068C78
	public Color GetColor(string name)
	{
		for (int i = 0; i < this.datas.Length; i++)
		{
			if (this.datas[i].param.type == MaterialCustoms.Parameter.TYPE.COLOR && this.datas[i].param.name == name)
			{
				MaterialCustoms.Data_Color data_Color = this.datas[i] as MaterialCustoms.Data_Color;
				return data_Color.Value;
			}
		}
		return Color.white;
	}

	// Token: 0x060010C7 RID: 4295 RVA: 0x0006AAEC File Offset: 0x00068CEC
	public float GetFloat(string name)
	{
		for (int i = 0; i < this.datas.Length; i++)
		{
			if (this.datas[i].param.name == name)
			{
				if (this.datas[i].param.type == MaterialCustoms.Parameter.TYPE.FLOAT01 || this.datas[i].param.type == MaterialCustoms.Parameter.TYPE.FLOAT01)
				{
					MaterialCustoms.Data_Float data_Float = this.datas[i] as MaterialCustoms.Data_Float;
					return data_Float.Value;
				}
				if (this.datas[i].param.type == MaterialCustoms.Parameter.TYPE.ALPHA)
				{
					MaterialCustoms.Data_Alpha data_Alpha = this.datas[i] as MaterialCustoms.Data_Alpha;
					return data_Alpha.Value;
				}
			}
		}
		return 0f;
	}

	// Token: 0x040011E1 RID: 4577
	public MaterialCustoms.Parameter[] parameters;

	// Token: 0x040011E2 RID: 4578
	public MaterialCustoms.Data_Base[] datas;

	// Token: 0x02000287 RID: 647
	[Serializable]
	public class Parameter
	{
		// Token: 0x060010C8 RID: 4296 RVA: 0x0006ABA8 File Offset: 0x00068DA8
		public Parameter()
		{
		}

		// Token: 0x060010C9 RID: 4297 RVA: 0x0006ABB0 File Offset: 0x00068DB0
		public Parameter(string name, MaterialCustoms.Parameter.TYPE type, string[] materialNames, string propertyName)
		{
			this.name = name;
			this.type = type;
			this.materialNames = new string[materialNames.Length];
			for (int i = 0; i < materialNames.Length; i++)
			{
				this.materialNames[i] = materialNames[i];
			}
			this.propertyName = propertyName;
		}

		// Token: 0x060010CA RID: 4298 RVA: 0x0006AC08 File Offset: 0x00068E08
		public Parameter(MaterialCustoms.Parameter copy)
		{
			this.name = copy.name;
			this.type = copy.type;
			this.materialNames = new string[copy.materialNames.Length];
			for (int i = 0; i < this.materialNames.Length; i++)
			{
				this.materialNames[i] = copy.materialNames[i];
			}
			this.propertyName = copy.propertyName;
		}

		// Token: 0x040011E3 RID: 4579
		public string name;

		// Token: 0x040011E4 RID: 4580
		public MaterialCustoms.Parameter.TYPE type;

		// Token: 0x040011E5 RID: 4581
		public string[] materialNames;

		// Token: 0x040011E6 RID: 4582
		public string propertyName;

		// Token: 0x02000288 RID: 648
		public enum TYPE
		{
			// Token: 0x040011E8 RID: 4584
			COLOR,
			// Token: 0x040011E9 RID: 4585
			FLOAT01,
			// Token: 0x040011EA RID: 4586
			FLOAT11,
			// Token: 0x040011EB RID: 4587
			ALPHA
		}
	}

	// Token: 0x02000289 RID: 649
	public class Data_Base
	{
		// Token: 0x060010CB RID: 4299 RVA: 0x0006AC7C File Offset: 0x00068E7C
		public Data_Base(MaterialCustoms.Parameter param, Renderer[] rends)
		{
			this.propertyID = Shader.PropertyToID(param.propertyName);
			this.param = param;
			for (int i = 0; i < rends.Length; i++)
			{
				for (int j = 0; j < rends[i].sharedMaterials.Length; j++)
				{
					string materialName = this.GetMaterialName(rends[i].sharedMaterials[j]);
					for (int k = 0; k < param.materialNames.Length; k++)
					{
						if (materialName == param.materialNames[k])
						{
							this.materials.Add(rends[i].materials[j]);
						}
					}
				}
			}
		}

		// Token: 0x060010CC RID: 4300 RVA: 0x0006AD34 File Offset: 0x00068F34
		private string GetMaterialName(Material material)
		{
			string text = material.name;
			int num = text.IndexOf(" (Instance)");
			if (num != -1)
			{
				text = text.Remove(num);
			}
			return text;
		}

		// Token: 0x040011EC RID: 4588
		public MaterialCustoms.Parameter param;

		// Token: 0x040011ED RID: 4589
		protected List<Material> materials = new List<Material>();

		// Token: 0x040011EE RID: 4590
		protected int propertyID;
	}

	// Token: 0x0200028A RID: 650
	public class Data_Color : MaterialCustoms.Data_Base
	{
		// Token: 0x060010CD RID: 4301 RVA: 0x0006AD64 File Offset: 0x00068F64
		public Data_Color(MaterialCustoms.Parameter param, Renderer[] rends) : base(param, rends)
		{
			if (this.materials.Count > 0)
			{
				if (this.materials[0].HasProperty(this.propertyID))
				{
					this.value = this.materials[0].GetColor(this.propertyID);
				}
				else
				{
					GlobalData.Debug_LogWarning("没有着色属性:" + this.param.propertyName + " " + this.materials[0].name);
				}
			}
		}

		// Token: 0x17000193 RID: 403
		// (get) Token: 0x060010CE RID: 4302 RVA: 0x0006ADF8 File Offset: 0x00068FF8
		// (set) Token: 0x060010CF RID: 4303 RVA: 0x0006AE00 File Offset: 0x00069000
		public Color Value
		{
			get
			{
				return this.value;
			}
			set
			{
				this.SetValue(value);
			}
		}

		// Token: 0x060010D0 RID: 4304 RVA: 0x0006AE0C File Offset: 0x0006900C
		public void SetValue(Color value)
		{
			this.value = value;
			for (int i = 0; i < this.materials.Count; i++)
			{
				this.materials[i].SetColor(this.propertyID, value);
			}
		}

		// Token: 0x040011EF RID: 4591
		protected Color value;
	}

	// Token: 0x0200028B RID: 651
	public class Data_Float : MaterialCustoms.Data_Base
	{
		// Token: 0x060010D1 RID: 4305 RVA: 0x0006AE54 File Offset: 0x00069054
		public Data_Float(MaterialCustoms.Parameter param, Renderer[] rends, float min, float max) : base(param, rends)
		{
			this.min = min;
			this.max = max;
			if (this.materials.Count > 0)
			{
				if (this.materials[0].HasProperty(this.propertyID))
				{
					this.value = this.materials[0].GetFloat(this.propertyID);
				}
				else
				{
					GlobalData.Debug_LogWarning("没有着色属性:" + this.param.propertyName);
				}
			}
		}

		// Token: 0x17000194 RID: 404
		// (get) Token: 0x060010D2 RID: 4306 RVA: 0x0006AEEC File Offset: 0x000690EC
		// (set) Token: 0x060010D3 RID: 4307 RVA: 0x0006AEF4 File Offset: 0x000690F4
		public float Value
		{
			get
			{
				return this.value;
			}
			set
			{
				this.SetValue(value);
			}
		}

		// Token: 0x17000195 RID: 405
		// (get) Token: 0x060010D4 RID: 4308 RVA: 0x0006AF00 File Offset: 0x00069100
		public float Min
		{
			get
			{
				return this.min;
			}
		}

		// Token: 0x17000196 RID: 406
		// (get) Token: 0x060010D5 RID: 4309 RVA: 0x0006AF08 File Offset: 0x00069108
		public float Max
		{
			get
			{
				return this.max;
			}
		}

		// Token: 0x060010D6 RID: 4310 RVA: 0x0006AF10 File Offset: 0x00069110
		public void SetValue(float value)
		{
			this.value = value;
			for (int i = 0; i < this.materials.Count; i++)
			{
				this.materials[i].SetFloat(this.propertyID, value);
			}
		}

		// Token: 0x040011F0 RID: 4592
		protected float value;

		// Token: 0x040011F1 RID: 4593
		protected float min;

		// Token: 0x040011F2 RID: 4594
		protected float max = 1f;
	}

	// Token: 0x0200028C RID: 652
	public class Data_Alpha : MaterialCustoms.Data_Base
	{
		// Token: 0x060010D7 RID: 4311 RVA: 0x0006AF58 File Offset: 0x00069158
		public Data_Alpha(MaterialCustoms.Parameter param, Renderer[] rends) : base(param, rends)
		{
			if (this.materials.Count > 0)
			{
				if (this.materials[0].HasProperty(this.propertyID))
				{
					this.value = this.materials[0].GetColor(this.propertyID).a;
				}
				else
				{
					GlobalData.Debug_LogWarning("没有着色属性:" + this.param.propertyName);
				}
			}
		}

		// Token: 0x17000197 RID: 407
		// (get) Token: 0x060010D8 RID: 4312 RVA: 0x0006AFE0 File Offset: 0x000691E0
		// (set) Token: 0x060010D9 RID: 4313 RVA: 0x0006AFE8 File Offset: 0x000691E8
		public float Value
		{
			get
			{
				return this.value;
			}
			set
			{
				this.SetValue(value);
			}
		}

		// Token: 0x060010DA RID: 4314 RVA: 0x0006AFF4 File Offset: 0x000691F4
		public void SetValue(float value)
		{
			this.value = value;
			for (int i = 0; i < this.materials.Count; i++)
			{
				Color color = this.materials[i].GetColor(this.propertyID);
				color.a = value;
				this.materials[i].SetColor(this.propertyID, color);
			}
		}

		// Token: 0x040011F3 RID: 4595
		protected float value;
	}
}
